/*
* @Author: jack.lu
* @Date: 2020/5/14
* @Last Modified by: jack.lu
* @Last Modified time: 2020/5/14 10:23 上午
*/

import config from "./../AliyunOSSConfig";

require('./hmac');
require('./sha1');
const Base64 = require('./base64');
const Crypto = require('./crypto');

function AliyunSignatureGenerator(expiration, contentLengthRange) {

    this.expiration = expiration;
    this.contentLengthRange = contentLengthRange;

    this.getSignature = function () {
        const bytes = Crypto.HMAC(Crypto.SHA1, this.getPolicyBase64(), config.AccessKeySecret, {
            asBytes: true,
        });

        return Crypto.util.bytesToBase64(bytes);
    }

    this.getPolicyBase64 = function () {
        const policyText = {
            expiration: "2021-07-01T12:00:00.000Z", // 设置该Policy的失效时间
            conditions: [
                ['content-length-range', 0, 50 * 1024 * 1024], // 设置上传文件的大小限制,50MB
            ],
        };
        return Base64.encode(JSON.stringify(policyText));
    }
}


export default new AliyunSignatureGenerator();